package com.shop2cn.iapi.couponmanage.testcase.old;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.coupon.service.UserReceiveCouponCall;
import com.shop2cn.iapi.couponmanage.parameter.CouponInfo;
import com.shop2cn.iapi.couponmanage.parameter.GetCouponsOfBatchByPagingBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.GetCouponsOfBatchByPagingCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.coupon;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.utils.TestDataManager;
//import com.ymttest.database.model.coupon;
//import com.ymttest.database.model.couponBatch;
//import com.ymttest.database.model.couponPrivateUserBound;
//import com.ymttest.database.model.couponValue;
//import com.ymttest.database.sqlwapper.couponBatchWapper;
//import com.ymttest.database.sqlwapper.couponPrivateUserBoundWapper;
//import com.ymttest.database.sqlwapper.couponValueWapper;
//import com.ymttest.database.sqlwapper.couponWapper;
/**
*分页获取指定批次下的优惠券列表信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCouponsOfBatchByPaging{
	private static GetCouponsOfBatchByPagingBean getcouponsofbatchbypagingBean;
	private static GetCouponsOfBatchByPagingCall getcouponsofbatchbypagingCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分页获取指定批次下的优惠券列表信息");
	}

	@Before
	public void caseUp() {
		getcouponsofbatchbypagingBean=new GetCouponsOfBatchByPagingBean();
		getcouponsofbatchbypagingCall=new GetCouponsOfBatchByPagingCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcouponsofbatchbypagingCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponsOfBatchByPaging_001() {
		Logger.start(true,"分页获取指定批次下的优惠券列表信息-总数小于当页最大条数");
		try {
			
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			CouponShop2cnMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(), TestDataManager.getRandomIntNum(6));
			CouponShop2cnMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(), TestDataManager.getRandomIntNum(6));
			
			coupon coupon = new couponWapper().selectTop1();
			getcouponsofbatchbypagingBean.setBatchId(coupon.getBatchId());
			getcouponsofbatchbypagingBean.setPageSize(10);
			getcouponsofbatchbypagingBean.setPageIndex(1);
			
			getcouponsofbatchbypagingCall.setData(getcouponsofbatchbypagingBean);
			getcouponsofbatchbypagingCall.callService();
			
			Logger.verifyEquals("0", getcouponsofbatchbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponsofbatchbypagingCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponsofbatchbypagingCall.getCouponInfos().getCouponInfos();
			Logger.verifyEquals(2, getcouponsofbatchbypagingCall.getRecordCount(), "验证返回recordCount");
			Logger.verifyEquals(couponInfos.size(), getcouponsofbatchbypagingCall.getRecordCount(), "验证返回recordCount与couponInfos集合总数一致");
			
			for (int i=0;i<couponInfos.size();i++) {
				 CouponInfo couponInfo = couponInfos.get(i);
				// 验证返回数据正确性
				coupon scoupon=new couponWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				// 验证返回数据的正确性
				Logger.verifyEquals(scoupon.getUserId(), couponInfo.getUserId(), "验证返回UserId");
				int remainUseTimes=scoupon.getIsUsed()==true?0:1;
				Logger.verifyEquals(remainUseTimes, couponInfo.getRemainUseTimes(), "验证返回RemainUseTimes");
				
				SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				Logger.verifyEquals(time.format(scoupon.getAddTime()).subSequence(0, 19), couponInfo.getReceiveTime().replace("T", " "), "验证返回RemainUseTimes");
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponsOfBatchByPaging_002() {
		Logger.start(true,"分页获取指定批次下的优惠券列表信息-总数大于当页最大条数");
		try {
			
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			UserReceiveCouponCall UserReceiveCouponCall1=CouponShop2cnMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(), TestDataManager.getRandomIntNum(6));
			UserReceiveCouponCall UserReceiveCouponCall2=CouponShop2cnMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(), TestDataManager.getRandomIntNum(6));
			
			coupon coupon = new couponWapper().selectTop1();
			getcouponsofbatchbypagingBean.setBatchId(coupon.getBatchId());
			getcouponsofbatchbypagingBean.setPageSize(1);
			getcouponsofbatchbypagingBean.setPageIndex(1);
			
			getcouponsofbatchbypagingCall.setData(getcouponsofbatchbypagingBean);
			getcouponsofbatchbypagingCall.callService();
			
			Logger.verifyEquals("0", getcouponsofbatchbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponsofbatchbypagingCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponsofbatchbypagingCall.getCouponInfos().getCouponInfos();
			Logger.verifyEquals(2, getcouponsofbatchbypagingCall.getRecordCount(), "验证返回recordCount");
			//Logger.verifyEquals(couponInfos.size(), getcouponsofbatchbypagingCall.getRecordCount(), "验证返回recordCount与couponInfos集合总数一致");
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);
				// 验证返回数据正确性
				coupon scoupon=new couponWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				// 验证返回数据的正确性
				Logger.verifyEquals(scoupon.getUserId(), couponInfo.getUserId(), "验证返回UserId");
				int remainUseTimes=scoupon.getIsUsed()==true?0:1;
				Logger.verifyEquals(remainUseTimes, couponInfo.getRemainUseTimes(), "验证返回RemainUseTimes");
				SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				Logger.verifyEquals(time.format(scoupon.getAddTime()).subSequence(0, 19), couponInfo.getReceiveTime().replace("T", " "), "验证返回RemainUseTimes");
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponsOfBatchByPaging_003() {
		Logger.start(true,"分页获取指定批次下的优惠券列表信息-批次为空");
		try {
			getcouponsofbatchbypagingBean.setPageSize(10);
			getcouponsofbatchbypagingBean.setPageIndex(1);
			
			getcouponsofbatchbypagingCall.setData(getcouponsofbatchbypagingBean);
			getcouponsofbatchbypagingCall.callService();
			Logger.verifyEquals("2", getcouponsofbatchbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, getcouponsofbatchbypagingCall.getMessage().contains("批次不存在"), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}